#!/bin/bash
# resner@e.email, resner@notabug.org

help="
downloader.sh		mount a device.

	[args]

	-h			show help.
	-m (link)		download something using the music downloader.
	-v (link)		download something using the video downloader.
	-f (link)		download something using the file downloader.
"

music_downloader="yt-dlp -x --audio-format flac --audio-quality 0 -f bestaudio"
video_downloader="yt-dlp"
file_downloader="aria2c --max-connection-per-server 8"

error() {
	printf "\e[31mError\e[0m: %s\n" "$1"
}

print() {
	printf "%s\n" "$1"
}

while getopts hm:v:f: option; do
	case $option in
	h)
		print "$help"
		exit
		;;
	m)
		if [[ "$OPTARG" == *"watch?"* ]]; then
			$music_downloader "https://youtube.com/watch?${OPTARG#*"watch?"}"
		elif [[ "$OPTARG" == *"playlist?"* ]]; then
			$music_downloader "https://youtube.com/playlist?${OPTARG#*"playlist?"}"
		else
			$music_downloader "$OPTARG"
		fi
		;;
	v)
		if [[ "$OPTARG" == *"watch?"* ]]; then
			$video_downloader "https://youtube.com/watch?${OPTARG#*"watch?"}"
		elif [[ "$OPTARG" == *"playlist?"* ]]; then
			$video_downloader "https://youtube.com/playlist?${OPTARG#*"playlist?"}"
		else
			$video_downloader "$OPTARG"
		fi
		;;
	f)
		$file_downloader "$OPTARG"
		;;
	\?)
		error "Unknown flag passed."
		exit 1
		;;
	esac
done
